<?php
/**
* 压缩包模块 工具类
*
* 压缩包模块 工具类的注意及说明
* 以下use模块请不要互相伤害
*
* @author    xiecz <xiecz@yunys.com.cn>
* @copyright 2016 QuanZhouYunYiShang Inc.
*/
namespace App\Services\Tool;

use ZipArchive, Log;

class Zip{

    protected $ERROR= [
                            '00' => 'success',
                            '01' => 'param <file> is not found',
                            '02' => 'param <file> formate error',
                            '03' => 'function unlink old zip file [fault]',
                            '04' => 'create file .zip [fault]',
                            '05' => 'create dir in zip [fault]',
                            '06' => 'addFile [fault]'
                         ];



    /**
        生成压缩包
        参数：
            zip_name            压缩包名
            type                模块
            file                文件数组
     */
    function createZip($input){

        if(!isset($input['file'])){
            return $this->getErrorMsg('01');
        }else if(!is_array($input['file'])){
            return $this->getErrorMsg('02');
        }

        $input['zip_name'] = empty($input['zip_name'])?(md5(time().mt_rand(0,1000)).'.zip'):$input['zip_name'];
        $input['type'] = empty($input['type'])?'0':$input['type'];

        if(!is_dir(public_path().DIRECTORY_SEPARATOR.'images'.
                                 DIRECTORY_SEPARATOR.'zip'))
            mkdir(public_path().DIRECTORY_SEPARATOR.'images'.
                                DIRECTORY_SEPARATOR.'zip');

        if(!is_dir(public_path().DIRECTORY_SEPARATOR.'images'.
                                 DIRECTORY_SEPARATOR.'zip'.
                                 DIRECTORY_SEPARATOR.$input['type']))
            mkdir(public_path().DIRECTORY_SEPARATOR.'images'.
                                DIRECTORY_SEPARATOR.'zip'.
                                DIRECTORY_SEPARATOR.$input['type']);

        if(is_file(public_path().DIRECTORY_SEPARATOR.'images'.
                                 DIRECTORY_SEPARATOR.'zip'.
                                 DIRECTORY_SEPARATOR.$input['type'].
                                 DIRECTORY_SEPARATOR.$input['zip_name'])){
            try{
                $result = unlink(public_path().DIRECTORY_SEPARATOR.'images'.
                                               DIRECTORY_SEPARATOR.'zip'.
                                               DIRECTORY_SEPARATOR.$input['type'].
                                               DIRECTORY_SEPARATOR.$input['zip_name']);
            }catch(\Exception $e){
                $err = $this->getErrorMsg('03');
                $this->logError($err);
                return $err;
            }

        }

        $zip = new ZipArchive();
        $filename = public_path().DIRECTORY_SEPARATOR.'images'.
                                  DIRECTORY_SEPARATOR.'zip'.
                                  DIRECTORY_SEPARATOR.$input['type'].
                                  DIRECTORY_SEPARATOR.$input['zip_name'];
        $open = false;
        try{
            $open = $zip->open($filename,ZipArchive::CREATE);
        }catch(\Exception $e){
            $err = $this->getErrorMsg('04');
            $this->logError($err,$e);
            return $err;
        }

        $err = $this->getErrorMsg('00');

        if($open === TRUE){
            foreach($input['file'] as $k=>$v){

                $add_dir = false;
                $dir_name = $k;

                try{
                    $add_dir = $zip->addEmptyDir($dir_name);
                    if(!$add_dir){
                        $err = $this->getErrorMsg('05');
                        Throw new Exception($err['msg'],$err['state']);
                    }

                }catch(\Exception $e){
                    $err = $this->getErrorMsg('05');
                    $this->logError($err,$e);
                    return $err;
                }

                try{

                    foreach($v as $key=>$value){
                        $in_dir = $zip->addEmptyDir($dir_name . DIRECTORY_SEPARATOR . $key);
                        if(!$in_dir){
                            $err = $this->getErrorMsg('05');
                            Throw new Exception($err['msg'],$err['state']);
                        }

                        foreach($value as $ek=>$ev){
                            //添加至zip
                            if(is_file(public_path('images/'.$ev))){
                                $zip->addFile(public_path('images/'.$ev),$dir_name . DIRECTORY_SEPARATOR . $key . DIRECTORY_SEPARATOR .$ek.'.png');
                            }
                        }

                    }
                }catch(\Exception $e){
                    $err = $this->getErrorMsg('06');
                    $this->logError($err,$e);
                    return $err;
                }
            }
        }
        $zip->close();

        $url = asset('images/zip/'.$input['type'].'/'.$input['zip_name']);
        $err['url'] = $url;

        return $err;
    }

    private function logError($err,$e){
        Log::info('ZIP Log Error begin --------------');
        Log::info('body: '.$err['msg']);
        Log::info($e);
        Log::info('ZIP Log Error end-----------------');
        return ;
    }

    private function getErrorMsg($state){
        $result['state'] = $state;
        $result['msg'] = isset($this->ERROR[$state])?$this->ERROR[$state]:'';
        return $result;
    }


}
